package com.etc.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.etc.dao.UserMapper;
import com.etc.pojo.user.User;
import com.etc.service.UserService;
import com.etc.utils.MD5Utils;
import com.etc.utils.MessageAcquisition;
import com.etc.utils.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findByAll() {
        return userMapper.findByAll();
    }

    @Override
    public Integer deleteById(int id) {
        return userMapper.deleteById(id);

    }

    @Override
    public User findByUsername(User user) {
        if (user == null || "".equals(user.getUsername()) || "".equals(user.getPassword())) {
            log.info("参数缺失");
            return null;
        }
        try {
            List<User> byUsername = userMapper.findByUsername(user.getUsername());
            if (byUsername != null && !byUsername.isEmpty()) {
                User user1 = byUsername.get(0);
                if (user1.getPassword().equals(MD5Utils.getPwd(user.getPassword()))) {
                    return user1;
                } else {
                    return null;
                }
            }
            return null;
        } catch (Exception e) {
            e.fillInStackTrace();
            log.error("根据用户名查询报错");
            return null;
        }

    }

    @Override
    public Integer insertUser(User user) {
        Integer insert = null;
        try {
            insert = userMapper.insertUser(user);
        } catch (Exception e) {
            e.fillInStackTrace();
        }
        return insert;
    }



    @Override
    public PageInfo<User> findByPage(int pageNo, int pageSize) {
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        List<User> userList = findByAll();//获取所有用户信息
        return new PageInfo<User>(userList);
    }

    @Override
    public String messageAcquisition(String phoneNumbers) throws ClientException {
        //Java 随机数
        String yanzmeng = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        SendSmsResponse sendSmsResponse = MessageAcquisition.messageAcquisition(phoneNumbers, yanzmeng);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            //保存到redis
            Jedis jedis = RedisUtil.getJedis();
            //jedis 定时
            String setex = jedis.setex(phoneNumbers, 60000, yanzmeng);
            if (!setex.isEmpty()) {
                RedisUtil.still(jedis);
                return "1";

            } else {
                return "2";
            }
        } else {
            return "2";
        }
    }

    @Override
    public Integer yanzheng(String phoneNumbers, String yanzheng) {
        if(phoneNumbers == null || "".equals(phoneNumbers)){
            return 1;
        }
        if(yanzheng == null || "".equals(yanzheng)){
            return 2;
        }
        Jedis jedis = RedisUtil.getJedis();
        String s = jedis.get(phoneNumbers);
        if(s == null || "".equals(s)){
            return 3;
        }else{
            if(s.equals(yanzheng)){
                return 4;
            }else{
                return 5;
            }
        }


    }


}
